package com.atguigu.ggkt.vod.controller;

import com.atguigu.ggkt.response.Result;
import com.atguigu.ggkt.vod.service.VodService;
import com.atguigu.ggkt.vod.utils.ConstantPropertiesUtil;
import com.atguigu.ggkt.vod.utils.Signature;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

/**
 * @author zfj
 * @date 2022/7/10 -13:37
 */
@Api(tags = "腾讯云点播")
@RestController
@RequestMapping("/admin/vod")
public class VodController {
	@Autowired
	private VodService vodService;

	@GetMapping("sign")
	public Result sign() {
		Signature sign = new Signature();
		// 设置 App 的云 API 密钥
		sign.setSecretId(ConstantPropertiesUtil.ACCESS_KEY_ID);
		sign.setSecretKey(ConstantPropertiesUtil.ACCESS_KEY_SECRET);
		sign.setCurrentTime(System.currentTimeMillis() / 1000);
		sign.setRandom(new Random().nextInt(java.lang.Integer.MAX_VALUE));
		sign.setSignValidDuration(3600 * 24 * 2); // 签名有效期：2天
		try {
			String signature = sign.getUploadSignature();
			System.out.println("signature : " + signature);
			return Result.ok(signature);
		} catch (Exception e) {
			System.out.print("获取签名失败");
			e.printStackTrace();
			return Result.fail(null);
		}
	}

	//上传视频
	@PostMapping("/upload")
	public Result uploadVideo(
			@ApiParam(name = "file", value = "文件", required = true)
			@RequestParam("file") MultipartFile file) throws IOException
	{
//		InputStream inputStream = file.getInputStream();
//		String originalFilename = file.getOriginalFilename();
		String videoId = vodService.uploadVideo();
		return Result.ok(videoId);
	}

	//删除视频
	@DeleteMapping("remove/{videoSourceId}")
	public Result removeVideo( @PathVariable String videoSourceId) {
		vodService.removeVideo(videoSourceId);
		return Result.ok();
	}

}
